package com.supersit.greensproject.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.supersit.greensproject.R;
import com.supersit.greensproject.entity.PriceGather;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chen on 2016/7/25.
 */
public class PriceGatherAdapter extends BaseAdapter {
    private Context context;
    private List<PriceGather> data;
    private String[] maketTypes;

    public PriceGatherAdapter(Context context, List<PriceGather> data) {
        this.context = context;
        this.data = data;
        maketTypes = context.getResources().getStringArray(R.array.maket_type);
    }

    @Override
    public int getCount() {
        return null == data ? 0 : data.size();
    }

    @Override
    public Object getItem(int position) {
        return null == data ? null : data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (null == convertView) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_price_gather, null);
            holder.tvGatherDate = (TextView) convertView.findViewById(R.id.tv_price_gather_date);
            holder.tvMakteType = (TextView) convertView.findViewById(R.id.tv_select_maket_type);
            holder.tvPrice = (TextView) convertView.findViewById(R.id.tv_price);
            holder.tvProductName = (TextView) convertView.findViewById(R.id.tv_product_name);
            holder.tvMaketName = (TextView) convertView.findViewById(R.id.tv_price_gather_maket_name);
            holder.isDelete = (ImageView) convertView.findViewById(R.id.iv_is_delete);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tvGatherDate.setText(data.get(position).getCreateDate());
        holder.tvMakteType.setText(maketTypes[data.get(position).getMaketType()]);
        holder.tvPrice.setText(data.get(position).getAveragePrice());
        holder.tvProductName.setText(data.get(position).getProductName());
        holder.tvMaketName.setText(data.get(position).getMaketName());
        if (data.get(position).getOperationsTatus() == 0) {
            holder.isDelete.setVisibility(View.VISIBLE);
        } else {
            holder.isDelete.setVisibility(View.GONE);
        }
        return convertView;
    }

    class ViewHolder {
        TextView tvGatherDate;
        TextView tvMakteType;
        TextView tvProductName;
        TextView tvPrice;
        TextView tvMaketName;
        ImageView isDelete;
    }

    public void setData(List<PriceGather> data) {
        this.data = data;
    }
}
